'##################################################
'###  Top Level (Get root Path from system env)
'##################################################
function GetEnv(envVarName)
	On Error Resume Next
	' 创建Shell对象并获取值
	Set WshShell = CreateObject("WScript.Shell")
	envValue = WshShell.Environment("PROCESS")(envVarName) ' 优先查找进程级变量

	' 如果未找到则尝试系统级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("SYSTEM")(envVarName)
	End If

	' 如果仍未找到则尝试用户级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("USER")(envVarName)
	End If

	' 检查结果并输出
	If Err.Number = 0 And envValue <> "" Then
		GetEnv=envValue
	Else
		GetEnv=""
	End If
end function

'##################################################
'##################################################
'##################################################
sub IncludeVbs(filepath)
	Dim fso, file, content
	Set fso = CreateObject("Scripting.FileSystemObject")
	' 读取functions.vbs的内容
	Set file = fso.OpenTextFile(filepath, 1) ' 1表示只读
	content = file.ReadAll
	file.Close
	' 将functions.vbs的内容载入全局作用域
	ExecuteGlobal content
end sub

'##################################################
'##################################################
'##################################################
dim root,tmpfile
Dim doc
Set doc = application.ActiveDocument
dim xytfile,xybfile,comp_top_file,comp_ref_top_file,comp_bot_file,comp_ref_bot_file
dim userlayername :userlayername="check_ref"
dim fso :Set fso = CreateObject("Scripting.FileSystemObject")
main
set doc = nothing
set fso = nothing

sub main()	
	'// REUSE
	envVarName ="EXP_SHORTCUTS_ROOT"
	root = GetEnv(envVarName)'"C:\MentorGraphics\2005EXP\SDD_HOME\pvtk\env\"
	if root ="" then 
		msgbox "please set env value (EXP_SHORTCUTS_ROOT = ?)"
		exit sub
	end if
	
	call IncludeVbs(root & "\skill_lib.vbs")
	
	If ValidateServer(doc) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If	
	
	'// TODO
	doc.UnHighlightAll()
	doc.currentunit = 2
	dim ulyr,usrLyrObj
	ulyr = "zyq_chk"
	
	Dim pntsArr()
	ReDim pntsArr(1,1)
	Dim numPoints
	Dim colorPatternObj
	a = 255
	b = 0
	c = 0
	set colorPatternObj = application.Utility.NewColorPattern(a, b, c)	
	call CheckUserlyr_withColor(ulyr,colorPatternObj)
	Set usrLyrObj = doc.FindUserLayer(ulyr)
	
	dim cmp,cmps
	dim fab,fabs
	set cmps = doc.components(1)	
	dim x0,x1,y0,y1	
	for each cmp in cmps
		x0 =cmp.positionX
		y0 =cmp.positionY
		set fabs = cmp.FabricationLayerTexts(2,0,0)
		'msgbox fabs.count
		for each fab in fabs
			if fab.name = cmp.name then
				x1 = fab.positionX
				y1 = fab.positionY
				'msgbox x0 & "," & y0 & vbcrlf & x1 & "," & y1
				pntsArr(0,0) = x0
				pntsArr(1,0) = y0
				pntsArr(0,1) = x1
				pntsArr(1,1) = y1
				numPoints = UBound(pntsArr,2) +1
				call doc.PutUserLayerGfx(usrLyrObj,1.0,numPoints,pntsArr,False,Nothing,epcbUnitCurrent)
			end if			
		next
	next
	'msgbox "done"
end sub
'##################################################################################################
'##################################################################################################
'##################################################################################################
Private Function ValidateServer(docObj)
	Dim key, licenseServer, licenseToken
	key = docObj.Validate(0)
	Set licenseServer = CreateObject("MGCPCBAutomationLicensing.Application")
	licenseToken = licenseServer.GetToken(key)
	Set licenseServer = nothing
	Err.Clear
	docObj.Validate(licenseToken)
	If Err Then
	ValidateServer = 0
	Else
	ValidateServer = 1
	End If
End Function
